package com.hsy.crm.web.utils;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.App;

import java.sql.*;
import java.util.Random;

public class MysqlUtils {

    public static void main(String[] args){
        Connection conn = null;
        try {
            conn = getConnection();

            String sql = "INSERT INTO `hrms`.`t_order`(`id`, `code`, `order_id`, `order_type`, `relation_id`, `company_id`, `date`, `due_money`, `real_money`, `discount_money`, `debt_money`, `remarks`, `status`, `create_user_id`, `create_time`, `update_user_id`, `update_time`, `customer_id`) VALUES ";

            String detailSql = "INSERT INTO `hrms`.`t_order_detail`(`id`, `order_id`, `detail_type`, `product_id`, `company_id`, `store_id`, `unit_id`, `number`, `price`, `percent_price`, `discount_rate`, `discount_money`, `discount_price`, `remarks`, `ord`, `customer_id`) VALUES ";

            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder detail = new StringBuilder();

            int index = 0;

            for( int i=0 ; i<20000 ; i++ ){

                long id = UuidUtils.getUuid();
                int orderType = getOrderType();




                if( i>0 && i%2000==0){

                    stringBuilder.append(getOrderStr(id,";",orderType));
                    conn.prepareStatement(sql+stringBuilder.toString()).executeUpdate();
                    stringBuilder.setLength(0);
                    System.out.println(index);

                    for( int j=0,size=random.nextInt(5)+1 ; j< size; j++ ){
                        if(j==size-1){
                            detail.append(getOrderDetailStr(id,orderType,";"));
                        }else{
                            detail.append(getOrderDetailStr(id,orderType,","));
                        }

                    }
                    conn.prepareStatement(detailSql+detail.toString()).executeUpdate();
                    detail.setLength(0);

                    index ++;
                }else{
                    stringBuilder.append(getOrderStr(id,",",orderType));

                    for( int j=0 ; j<random.nextInt(5)+1 ; j++ ){
                        detail.append(getOrderDetailStr(id,orderType,","));
                    }
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeConn(conn);
        }

    }

    static Long orderNo = 201812100000001l;
    static Long[] productIds = new Long[]{1543373464695677204l
            ,1543373687124961853l
            ,1543373784768273782l
            ,1543373836786886770l
            ,1543373870314581012l
            ,1543373915033511893l
            ,1543373954122693639l
            ,1543373998866903712l
            ,1543374036149466083l
            ,1543374075420235504l
            ,1543374114666273658l
            ,1543374147150438737l};

    static Long[] storeIds = new Long[]{1536467919323406630l,
            1536467944776126410l,
            1536467982423050208l};

    private static String getOrderDetailStr(long orderId,int orderType,String ch){
        String value = "("+UuidUtils.getUuid()+", "+orderId+", "+getDetailType(orderType)+", "+getProductId()+", "+(20000000+random.nextInt(10))+", "+getStoreId()+", "+random.nextInt(17)+", "+getNumber(orderType)+", "+random.nextInt(10000)+", 0, 100, 0, "+random.nextInt(10000)+", '', 0, 100000001)"+ch;
        return value;
    }

    private static long getProductId(){
        return productIds[random.nextInt(12)];
    }

    private static int getNumber(int orderType){
        if( orderType == App.BILL_TYPE.ORDER_SHD ){
            return -random.nextInt(100);
        }else{
            return random.nextInt(100);
        }

    }

    private static long getStoreId(){
        return storeIds[random.nextInt(3)];
    }

    private static int getDetailType(int orderType){
        if( orderType == App.BILL_TYPE.ORDER_SHD ){
            return 0;
        }else{
            return 1;
        }
    }

    private static String getOrderStr(Long id,String ch,int orderType){
        String value = "("+id+", '"+orderNo+"', -1, "+orderType+", 5, 200000001, 1544400000000, 390, 0, 0, 390, '', 20, 300000001, 1544413219105, -1, -1, 100000001)"+ch;
        orderNo ++;
        return value;
    }


    static Random random = new Random();
    private static int getOrderType(){
        int result = random.nextInt(100);
        if( result % 2 == 0 ){
            return App.BILL_TYPE.ORDER_SHD;
        }else{
            return App.BILL_TYPE.ORDER_JHD;
        }
    }

    private static void closeRs(ResultSet rs){
        if( rs!=null ){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private static void closePstmt(PreparedStatement pstmt){
        if( pstmt!=null ){
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private static void closeConn(Connection connection){
        if( connection!=null ){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/hrms?characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false","root","root");
            //conn = DriverManager.getConnection("jdbc:mysql://192.168.79.152:3306/hrms?characterEncoding=UTF-8&useSSL=false","root","root");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
}
